package zouhaotian.rbac.service.array;

import zouhaotian.rbac.domain.dto.DeptDto;
import zouhaotian.rbac.domain.dto.RoleDto;
import zouhaotian.rbac.domain.entity.Dept;
import zouhaotian.rbac.domain.entity.Role;
import zouhaotian.rbac.domain.entity.User;
import zouhaotian.rbac.domain.vo.Deptvo;
import zouhaotian.rbac.domain.vo.Rolevo;
import zouhaotian.rbac.domain.vo.Result;
import zouhaotian.rbac.service.IRoleService;
import zouhaotian.rbac.service.IUserService;
import zouhaotian.rbac.util.SingletonFactory;

import java.time.LocalDateTime;


/**
 * 业务层-角色
 *
 * @auther 邹昊天
 * @since 2025/6/19 21:35
 */

@Deprecated
public class RoleService  extends BaseService<Role, RoleDto, Rolevo>implements IRoleService {


    private IUserService userService;



    private RoleService(){
        data = new Role[DATA_SIZE + 1];
        temp = new Rolevo[DATA_SIZE];
    }


    @Override
    Rolevo convertToVo(Role role){
        return new Rolevo(role.getId(), role.createAt, role.updateAt,
                role.name, role.dataScope);
    }

    @Override
    Role createEntity(RoleDto dto, int id){
        LocalDateTime now = LocalDateTime.now();
        return new Role(id, now, now, dto.name, dto.dataScope);
    }

    @Override
    void updateEntity(RoleDto dto, Role role){
        role.updateAt = LocalDateTime.now();
        role.name = dto.name;
        role.dataScope = dto.dataScope;
    }



    @Override
    public Result delete(int id){
        if (!existsById(id)){
            return Result.failure("删除失败，不存在该数据");
        }
        if (userService == null){
            userService = SingletonFactory.getInstance(UserService.class);
        }
        if (userService.roleInUse(id)){
            return Result.failure("删除失败，还有用户具有该角色，不能删除");
        }
        data[id] = null;
        return Result.success("删除成功", null);
    }
}
